package com.css.zfzx.fxpg.modules.casulaty.probability.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.fxpg.modules.casulaty.probability.repository.entity.FxCasulatyProbabilityEntity;
import com.css.zfzx.fxpg.modules.casulaty.probability.service.FxCasulatyProbabilityService;
import com.css.zfzx.fxpg.modules.casulaty.probability.viewobjects.FxCasulatyProbabilityQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 伤亡概率
 *
 * @author leon
 * @date 2020-12-03
 */
@Slf4j
@RestController
@RequestMapping("/fxCasulatyProbabilitys")
public class FxCasulatyProbabilityController {
    @Autowired
    private FxCasulatyProbabilityService fxCasulatyProbabilityService;

    @GetMapping("/queryFxCasulatyProbabilitys")
    public RestResponse queryFxCasulatyProbabilitys(HttpServletRequest request, FxCasulatyProbabilityQueryParams queryParams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = fxCasulatyProbabilityService.queryFxCasulatyProbabilitys(queryParams, curPage, pageSize);
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/listFxCasulatyProbabilitys")
    public RestResponse listFxCasulatyProbabilitys(FxCasulatyProbabilityQueryParams queryParams) {
        RestResponse response = null;
        try {
            List<FxCasulatyProbabilityEntity> list = fxCasulatyProbabilityService.listFxCasulatyProbabilitys(queryParams);
            response = RestResponse.succeed(list);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getFxCasulatyProbability(@PathVariable String id) {
        RestResponse response = null;
        try {
            FxCasulatyProbabilityEntity fxCasulatyProbability = fxCasulatyProbabilityService.getFxCasulatyProbability(id);
            response = RestResponse.succeed(fxCasulatyProbability);
        } catch (Exception e) {
            String errorMessage = "获取失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping
    public RestResponse saveFxCasulatyProbability(@RequestBody FxCasulatyProbabilityEntity fxCasulatyProbability) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxCasulatyProbabilityService.saveFxCasulatyProbability(fxCasulatyProbability);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "新增失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }

    @PutMapping
    public RestResponse updateFxCasulatyProbability(@RequestBody FxCasulatyProbabilityEntity fxCasulatyProbability) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            FxCasulatyProbabilityEntity entity = fxCasulatyProbabilityService.updateFxCasulatyProbability(fxCasulatyProbability);
            // 修改期望值
            fxCasulatyProbabilityService.updateExpect(entity);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PutMapping("/updateXYByY")
    public RestResponse updateXYByY(@RequestBody FxCasulatyProbabilityEntity fxCasulatyProbability) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            FxCasulatyProbabilityEntity entity = fxCasulatyProbabilityService.updateXYByY(fxCasulatyProbability);
            // 修改期望值
            fxCasulatyProbabilityService.updateExpect(entity);
            json.put("message", "计算成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            //String errorMessage = "计算失败!";
            e.printStackTrace();
            response = RestResponse.fail(e.getMessage());
        }
        return response;
    }

    @DeleteMapping
    public RestResponse deleteFxCasulatyProbabilitys(@RequestParam List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxCasulatyProbabilityService.deleteFxCasulatyProbabilitys(ids);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(fxCasulatyProbabilityService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    @GetMapping("/findByDictCodeAndDictItemCode")
    public RestResponse findByDictCodeAndDictItemCode(@RequestParam String dictCode, @RequestParam String dictItemCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(fxCasulatyProbabilityService.findByDictCodeAndDictItemCode(dictCode, dictItemCode));
        } catch (Exception e) {
            String errorMsg = "字典名获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 初始化
     *
     * @return
     */
    @GetMapping("/initialize")
    public RestResponse initialize() {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxCasulatyProbabilityService.initialize();
            json.put("message", "初始化成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 导出伤亡概率
     *
     * @param queryParams
     * @return
     */
    @GetMapping("/export")
    public RestResponse exportExcel(FxCasulatyProbabilityQueryParams queryParams, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = fxCasulatyProbabilityService.exportExcel(queryParams, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导入失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 导出T年伤亡估算
     *
     * @param queryParams
     * @return
     */
    @GetMapping("/exportT")
    public RestResponse exportTExcel(FxCasulatyProbabilityQueryParams queryParams, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = fxCasulatyProbabilityService.exportTExcel(queryParams, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导入失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 导入T年伤亡估算
     *
     * @param file
     * @return
     */
    @PostMapping("/importT")
    public RestResponse importTExcel(@RequestParam("file") MultipartFile file) {
        RestResponse restResponse = null;
        try {
            String s = fxCasulatyProbabilityService.importTExcel(file);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导入失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 导入伤亡概率
     *
     * @param file
     * @return
     */
    @PostMapping("/import")
    public RestResponse importExcel(@RequestParam("file") MultipartFile file) {
        RestResponse restResponse = null;
        try {
            String s = fxCasulatyProbabilityService.importExcel(file);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导入失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 计算
     *
     * @param queryParams
     * @return
     */
    @GetMapping("/calculate")
    public RestResponse calculate(FxCasulatyProbabilityQueryParams queryParams) {
        RestResponse restResponse = null;
        JSONObject object = new JSONObject();
        try {
            String s = fxCasulatyProbabilityService.calculate(queryParams);
            object.put("message", s);
            restResponse = RestResponse.succeed(object);
        } catch (Exception e) {
            String errorMsg = "计算失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }
}